package com.banshuibao.bsb_android.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.fragment.AnswerFragment;
import com.banshuibao.bsb_android.fragment.ExpertMineFragment;
import com.banshuibao.bsb_android.fragment.ExpertUserHomeFragment;
import com.banshuibao.bsb_android.fragment.VideoFragment;

public class ExpertUserHomeActivity extends BaseHomeActivity {
    /**
     * 专家 角色
     *
     * @param savedInstanceState
     */
    private ExpertUserHomeFragment homeFragment;
    private VideoFragment videoFragment;
    private AnswerFragment answerFragment;
    private ExpertMineFragment mineFragment;
    private FragmentTransaction transaction;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expert);
        manager = getSupportFragmentManager();
        initView();
        showFragment(R.id.home);
    }

    //对fragment进行隐藏
    public void hideFragment(FragmentTransaction ft) {
        //首页
        if (homeFragment != null) {
            ft.hide(homeFragment);
        }

        //视频
        if (videoFragment != null) {
            ft.hide(videoFragment);
        }

        //问答
        if (answerFragment != null) {
            ft.hide(answerFragment);
        }

        //我的
        if (mineFragment != null) {
            ft.hide(mineFragment);
        }

    }

    //显示具体哪个fragment
    @Override
    void showFragment(int id) {
        super.showFragment(id);
        Drawable drawable;
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        clearAnimation(home, video, answer, mine);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.icon_home_bar);
        switch (id) {
            case R.id.home:
                setDefault();
                home_layout.setSelected(true);
//                drawable = getResources().getDrawable(R.drawable.ic_file);
//                assert drawable != null;
//                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//                home.setCompoundDrawables(null, drawable, null, null);
                home.setTextColor(getResources().getColor(R.color.text_bottom_color));
                home_iv.setImageDrawable(getResources().getDrawable(R.drawable.home_f));
                home_iv.startAnimation(animation);
                if (homeFragment == null) {
                    homeFragment = new ExpertUserHomeFragment();
                    transaction.add(R.id.fl, homeFragment, "two");
                } else {
                    transaction.show(homeFragment);
                }
                break;
            case R.id.video:
                setDefault();
                video_layout.setSelected(true);
                video.setTextColor(getResources().getColor(R.color.text_bottom_color));
                video_iv.setImageDrawable(getResources().getDrawable(R.drawable.video_f));
//                drawable = getResources().getDrawable(R.drawable.ic_file);
//                assert drawable != null;
//                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//                video.setCompoundDrawables(null, drawable, null, null);
                video_iv.startAnimation(animation);
                if (videoFragment == null) {
                    videoFragment = new VideoFragment();
                    transaction.add(R.id.fl, videoFragment, "three");
                } else {
                    transaction.show(videoFragment);
                }
                break;
            case R.id.answer:
                setDefault();
                answer_layout.setSelected(true);
                answer.setTextColor(getResources().getColor(R.color.text_bottom_color));
                answer_iv.setImageDrawable(getResources().getDrawable(R.drawable.answer_f));
//                drawable = getResources().getDrawable(R.drawable.ic_file);
//                assert drawable != null;
//                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//                answer.setCompoundDrawables(null, drawable, null, null);
                answer_iv.startAnimation(animation);
                if (answerFragment == null) {
                    answerFragment = new AnswerFragment();
                    transaction.add(R.id.fl, answerFragment, "three");
                } else {
                    transaction.show(answerFragment);
                }
                break;
            case R.id.mine:
                setDefault();
                mine_layout.setSelected(true);
                mine.setTextColor(getResources().getColor(R.color.text_bottom_color));
                mine_iv.setImageDrawable(getResources().getDrawable(R.drawable.mine_f));
//                drawable = getResources().getDrawable(R.drawable.ic_file);
//                assert drawable != null;
//                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//                mine.setCompoundDrawables(null, drawable, null, null);
                mine_iv.startAnimation(animation);
                if (mineFragment == null) {
                    mineFragment = new ExpertMineFragment();
                    transaction.add(R.id.fl, mineFragment, "three");
                } else {
                    transaction.show(mineFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void clearAnimation(View... imgs) {
        for (View imageView : imgs) {
            if (imageView != null) {
                imageView.clearAnimation();
            }
        }
    }

    /**
     * 设置默认不选中
     */
    private void setDefault() {
        Drawable drawable;
        //首页
        home_layout.setSelected(false);
        home_iv.setImageDrawable(getResources().getDrawable(R.drawable.home_d));
        home.setTextColor(getResources().getColor(R.color.text_office_color));
//        drawable = getResources().getDrawable(R.drawable.ic_file);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//        home.setCompoundDrawables(null, drawable, null, null);
        //视频
        video_layout.setSelected(false);
        video_iv.setImageDrawable(getResources().getDrawable(R.drawable.video_d));
        video.setTextColor(getResources().getColor(R.color.text_office_color));
//        drawable = getResources().getDrawable(R.drawable.ic_file);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//        video.setCompoundDrawables(null, drawable, null, null);
        //问答
        answer_layout.setSelected(false);
        answer_iv.setImageDrawable(getResources().getDrawable(R.drawable.answer_d));
        answer.setTextColor(getResources().getColor(R.color.text_office_color));
//        drawable = getResources().getDrawable(R.drawable.ic_file);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//        answer.setCompoundDrawables(null, drawable, null, null);
        //我的
        mine_layout.setSelected(false);
        mine_iv.setImageDrawable(getResources().getDrawable(R.drawable.mine_d));
        mine.setTextColor(getResources().getColor(R.color.text_office_color));
//        drawable = getResources().getDrawable(R.drawable.ic_file);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//        mine.setCompoundDrawables(null, drawable, null, null);
    }

    @Override
    void showBottomMenu() {
        super.showBottomMenu();
        homeBottomMenu.setRole("0");
        homeBottomMenu.showExpertBottomMenu(getWindow().getDecorView());
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
       // super.onSaveInstanceState(outState);
    }
}
